package gob.pe.mtpe.struts.action;

import gob.pe.mtpe.ibatis.despachadores.AjaxDespatch;
import gob.pe.mtpe.struts.form.LoginForm;
import gob.pe.mtpe.util.funciones.Funciones;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import pe.gob.sunat.tecnologia.menu.bean.UsuarioBean;
 

public class LogueoAction extends DispatchAction{
    private static Logger depurador = Logger.getLogger(LogueoAction.class);
    private Funciones funciones;
    
    public ActionForward Login(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)  throws Exception {
       
    	
    	/*PARA MI MAQUINA LOCAL*/
    	String ruc = request.getParameter("v_ruc");
    	String usuarioSol = "LCBXFK2Z"; 
    	
    	/*PARA produccion*/
    	/*UsuarioBean usuarioBean = (UsuarioBean)request.getSession().getAttribute("usuarioBean"); 
    	String ruc             = usuarioBean.getNumRUC();
        String usuarioSol   = usuarioBean.getUsuarioSOL();*/
        
        
        LoginForm loginForm = (LoginForm)form;
     try{  
            AjaxDespatch dsp = new AjaxDespatch();
            loginForm.reset(mapping, request);            
            loginForm = dsp.obtDatos(ruc);
            if(loginForm==null){
            	return mapping.findForward("nodata");
            }
            loginForm.setV_hostreg(request.getRemoteHost());            
            loginForm.setV_usuario(usuarioSol); 
            request.getSession().setAttribute("SUSUARIO", loginForm);
            depurador.info("INGRESO INICIO ");
            return mapping.findForward("principal");            
         } catch (Exception e) {
            funciones=new Funciones();
            depurador.log(Level.ERROR," ERROR NO LLEGA EL RUC="+loginForm.getV_ruc()+"  NI EL USUARIO ="+loginForm.getV_usuario()+" ERROR  "+funciones.readStackTrace(e));
            return mapping.findForward("ErrorFatal");
        }
    }
     
    public ActionForward salir(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
            try{
                request.getSession().removeAttribute("SUSUARIO");
                request.getSession().removeAttribute("usuarioBean");
                request.getSession().invalidate();
               return mapping.findForward("salir");            
            } catch (Exception e) {
                funciones=new Funciones();
                depurador.log(Level.ERROR," ERROR SERRAR SESSION  "+funciones.readStackTrace(e));
                return mapping.findForward("ErrorFatal");
            }
        }
  }
